package com.algorithm.ch1.lzr.tree;

/**
 * 二叉树的最大深度
 * 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数
 * 叶子节点是指没有子节点的节点
 *
 * @author lzr
 * @date 2018/9/4
 */
public class MaxDepth {

    public static void main(String[] args) {
        TreeNode root = new TreeNode(0);
        TreeNode node1 = new TreeNode(1);
        TreeNode node2 = new TreeNode(2);
        TreeNode node3 = new TreeNode(3);
        TreeNode node4 = new TreeNode(4);
        TreeNode node5 = new TreeNode(5);
        TreeNode node6 = new TreeNode(6);

        root.left = node1;
        root.right = node2;
        node1.right = node3;
        node2.left = node4;
        node2.right = node5;
        node5.right = node6;

        int depth = maxDepth(root);
        System.out.println(depth);
    }

    /**
     * 获取二叉数的最大深度
     *
     * @param root
     * @return
     */
    public static int maxDepth(TreeNode root) {
        if(root ==null){
            System.out.println("root.val=null");
        }else{
            System.out.println("root.val="+root.val);
        }
        int depth = 0;
        if (root != null) {
            int leftDepth = maxDepth(root.left);
            int rightDepth = maxDepth(root.right);
            depth = leftDepth > rightDepth ? leftDepth + 1 : rightDepth + 1;
            System.out.println("===============depth.val="+depth);
        }
        return depth;
    }
}
